package org.melord.cap.lic;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Calendar;
import java.util.Date;

import org.melord.cap.Constants;

public class Validator {
	public int init(RTX rtx, License lic) {
		int code = -1;
		File f = new File(Constants.JNI_FILE);

		if (!f.exists()) {
			try {
				if (!f.createNewFile()) {
					return 1;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 15;
			}
		}

		try {
			ObjectOutputStream oos = new ObjectOutputStream(
					new FileOutputStream(f));
			oos.writeObject(rtx);
			oos.writeObject(lic);
			oos.writeObject(new Date());
			oos.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 1;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 15;
		}

		return code;
	}

	public int validate() {
		int code = -1;
		File f = new File(Constants.JNI_FILE);
		System.out.println(f.getAbsolutePath());
		if (!f.exists()) {
			return 1;
		} else if (!f.canRead()) {
			return 2;
		} else if (!f.canWrite()) {
			return 3;
		}

		try {
			ObjectInputStream ois = new ObjectInputStream(
					new FileInputStream(f));

			RTX rtx = (RTX) ois.readObject();
			License lic = (License) ois.readObject();
			ois.close();
			if (rtx.isInit()) {
				rtx.setInit(false);
				rtx.setStartTime(new Date());
				rtx.setRuninTime(new Date());
				save(rtx, lic);
				return 0x11;
			}
			// Date d = parseDate(rtx.getStartTime());
			Calendar c = Calendar.getInstance();
			c.setTime(rtx.getStartTime());

			// System.out.println("reg time:" +c.getTime());
			Calendar cc = Calendar.getInstance();
			cc.setTime(new Date());
			// System.out.println("cur time:" + cc.getTime());
			if (cc.before(c)) {
				return 0x21;
			}
			c.add(Calendar.DAY_OF_YEAR, lic.days);
			// System.out.println(c.getTime());
			if (c.before(cc)) {
				rtx.setRuninTime(new Date());
				return 0x22;
			}

			c.setTime(rtx.getRuninTime());
			c.roll(Calendar.DAY_OF_YEAR, -1);
			// System.out.println(c.getTime());
			if (cc.before(c)) {
				return 0x23;
			}
			rtx.setRuninTime(new Date());
			save(rtx, lic);
			return 0x12;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 1;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 15;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return 14;
		}

		// return code;
	}

	public int save(RTX rtx, License lic) {
		int code = -1;
		File f = new File(Constants.JNI_FILE);
		if (!f.exists()) {
			return 1;
		} else if (!f.canRead()) {
			return 2;
		} else if (!f.canWrite()) {
			return 3;
		}

		try {
			ObjectOutputStream oos = new ObjectOutputStream(
					new FileOutputStream(f));
			oos.writeObject(rtx);
			oos.writeObject(lic);
			oos.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 1;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 15;
		}

		return code;
	}

	public static void main(String[] args) {
		Validator validator = new Validator();
		License lic = new License("kkw3", "kdkuakl", "jkjiakjkdl", 60, 20);
		RTX rtx = new RTX();
		rtx.setInit(true);
		rtx.setStartTime(new Date());
		rtx.setRuninTime(new Date());
		validator.init(rtx, lic);
	}
}
